<?php
function navigation_bar($name, $add=0, $search=1) {
        global $langrecord, $langof, $langsearch, $langadd, $langtop, $langbottom, $langnext;
        global $langprevious, $langfiltered, $color3, $langrecperpage;

        // AG: records per page customizable
        if ($_GET['RecordPerPage'])
            $_SESSION['RecordPerPage'] = $_GET['RecordPerPage'];

        $program = $_SERVER['PHP_SELF'];
        $url_text = retrieve_search_list();

        if ($_GET['total_record'] != 0) $i = $_GET['record_offset'] + 1;
        else $i = 0;
        $text = "$langrecord : $i" ;

        $i = $_GET['record_offset'] + $_SESSION['RecordPerPage'];
        if ($i > $_GET['total_record']) $i = $_GET['total_record'];
        $text .= " - $i $langof " . $_GET['total_record'];

        // calculate offset position for PREVIOUS button
        if ($_GET['record_offset'] == 0) $previous = 0 ;
        else {
                $previous = $_GET['record_offset'] - $_SESSION['RecordPerPage'];
                if ($previous < 0) $previous = 0;
        }

        // calculate offset position for NEXT button
        $next = $_GET['record_offset'] + $_SESSION['RecordPerPage'] ;
        if ($next >= $_GET['total_record']) $next = $_GET['record_offset'];

        // calculate offset position for BOTTOM button
        $bottom = (ceil($_GET['total_record'] / $_SESSION['RecordPerPage']) - 1) * $_SESSION['RecordPerPage'] ;

        // display navigation hyperlink
        ?>
        <form name='navbar' action''>
        <table width='100%' border='1' bgcolor='<? echo $color3 ?>' cellspacing="0">
                <tr  bgcolor='<? echo $color3 ?>' align="center">
                        <td width='20%'><b><? echo $name ?>
                        <?
                        if ($search == 1) echo "<td width='10%'><a href = '$program'>$langsearch</a>";
                        if ($add == 1) echo "<td width = '10%'><a href = '$program?action=add_form'>$langadd</a>";
                        ?>
                        <td width='10%'>
                        <a href='<? echo $program ?>?action=browse&total_record=<? echo trim($_GET['total_record']) . $url_text ?>&record_offset=0'><? echo $langtop ?></a>

                        <td width='10%'>
                        <a href='<? echo $program ?>?action=browse&total_record=<? echo trim($_GET['total_record']) . $url_text ?>&record_offset=<? echo $previous ?>'><? echo $langprevious ?></a>

                        <td width='10%'>
                        <a href='<? echo $program ?>?action=browse&total_record=<? echo trim($_GET['total_record']) . $url_text ?>&record_offset=<? echo $next ?>'><? echo $langnext ?></a>

                        <td width='10%'>
                        <a href='<? echo $program ?>?action=browse&total_record=<? echo trim($_GET['total_record']) . $url_text ?>&record_offset=<? echo $bottom ?>'><? echo $langbottom ?></a>

                        <!-- AG: records per page customizable -->
                        <?
                        unset($sel_rpp);
                        $sel_rpp[$_SESSION['RecordPerPage']] = "SELECTED";
                        ?>
                        <td width='10%'><? echo $langrecperpage ?>
                        <select name='RecPerPage' onChange='location.href=location.href+"&RecordPerPage="+document.navbar.RecPerPage.value;'>
                                <option value=10   <?echo $sel_rpp["10"]  ?>>10</option>
                                <option value=20   <?echo $sel_rpp["20"]  ?>>20</option>
                                <option value=50   <?echo $sel_rpp["50"]  ?>>50</option>
                                <option value=100  <?echo $sel_rpp["100"] ?>>100</option>
                                <option value=200  <?echo $sel_rpp["200"] ?>>200</option>
                                <option value=500  <?echo $sel_rpp["500"] ?>>500</option>
                                <option value=1000 <?echo $sel_rpp["1000"]?>>1000</option>
                        </select>

                        <td width='10%'><? echo $text ?>
                        <?


                        // determine whether it is a filtered recordset
                        foreach ($_SESSION[$program] as $index => $value) {
                                if (! empty($value)) {
                                        $filtered = 1;
                                        break;
                                }
                        }
                        if ($filtered) echo "<td width='5%' bgcolor = 'red' align='center'>$langfiltered";
                        ?>
        </table><br>
        </form>
        <?
}

function title_bar($name, $add=0, $search=1) {
        global $langsearch, $langadd, $color3;

        ?>
        <table>
                <tr bgcolor='<? echo $color3 ?>' align="center">
                        <td abbr="20%" ><b><? echo $name ?>
                        <?
                        if ($search == 1)
                                echo "<td width='10%'><font color='$color4'><a href = '" . $_SERVER['PHP_SELF'] . "'>$langsearch</a>";
                        if ($add == 1)
                                echo "<td width = '10%'><a href = '" . $_SERVER['PHP_SELF'] ."?action=add_form'>$langadd</a>";
                        ?>
        </table><br>
        <?
}

function html_header() {
        global $company_logo, $charset, $langtitle, $langsignoff;
        ?>
        <html>
        <head>
        <title>TimeReport (<?php echo $_SERVER['PHP_SELF'] ?>)</title>
        <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=<? echo $charset ?>">
        <SCRIPT LANGUAGE="JavaScript" SRC="CalendarPopup.js"></SCRIPT>
        <SCRIPT LANGUAGE="JavaScript" SRC="FormUtils.js"></SCRIPT>
        </head>
        <LINK REL="StyleSheet" HREF="style.css" TYPE="text/css">
        <body>
        <!-- Created by Roger Pang on November 2002 - pangsiufai@hotmail.com -->
        <table width="100%">
                <tr>
                        <td><!--<IMG SRC='img/logo.gif' alt='TimeReport'>-->
                        	<h1>TI.E.R.</h1>
                        	<h2>Time & Expense Report</h2>
                        <td><?
                                if ($_SESSION['UserID']) {
                                        echo "<a href='#' " .
                                                 "onclick='window.open(\"staffmaster.php?action=modify_form\", " .
                                                 "\"\" ,\"toobars=no, scrollbars=1, width=800, height=540,left=5,top=5\")'>" .
                                                 $_SESSION['UserID'] . "</a>";
                                        if (strstr($_SERVER['PHP_SELF'], 'mainmenu')) {
                                                echo "<td><a href='signoff.php')>$langsignoff</a>";
                                        }
                                }
                        ?>
        </table>
        <?
}

function html_footer() {
        global $langcopyright ;
        ?>
        <br><br><a href='#' onclick="window.open('copyright.html', 'copyright' ,'toobars=no, scrollbars=1, width=600, height=540')">
        <? echo $langcopyright ?></a>
        </body>
        <?
}

function column_headings($columns) {
        global $color3;
        ?>
        <table>
        <tr bgcolor='<? echo $color3 ?>'>
                <?
                foreach ($columns as $header) {
                        echo "<td><b>$header";
                }
}

// AGMOD 01.02.2006: added calendar popup on date fields

function search_form($fields, $program) {
        global $langfind, $langfrom, $langto, $color3;
        ?>

        <SCRIPT LANGUAGE="JavaScript" ID="js1">
        var cal1 = new CalendarPopup();
        </SCRIPT>

        <form method="GET" action = '<? echo $program ?>'>
        <table>
                <tr bgcolor='<? echo $color3 ?>'>
                        <td>
                        <td align="center"><? echo $langfrom ?>
                        <td align="center"><? echo $langto ?>
                <?
                for ($i = 0; $i < count($fields) ; $i++) {
                        echo "\n<tr>";
                        echo "\n        <td bgcolor='$color3'>" . $fields[$i]['title'];
                        echo "\n        <td><input type = 'text' name = 'from_" .
                                 $fields[$i]['field'] . "' " .
                                 "maxlength = '" . $fields[$i]['length'] . "' " .
                                 "value = '" . $fields[$i]['defaultfrom'] . "'>";
                        // CALENDAR
                        if (substr_count(strtoupper($fields[$i]['field']),'DATE') >= 1) {
                            echo '<A HREF="#" onClick="cal1.select(document.forms[0].from_'.$fields[$i]['field'].',\'anchor1\',\'ddMMyyyy\'); return false;" TITLE="Data" NAME="anchor1" ID="anchor1">'
                                .'<img src="calendar.gif" border=0></A>';
                        }


                        echo "\n        <td><input type = 'text' name = 'to_" .
                                 $fields[$i]['field'] . "' " .
                                 "maxlength = '" . $fields[$i]['length'] . "' " .
                                 "value = '" . $fields[$i]['defaultto'] . "'>";
                        // CALENDAR
                        if (substr_count(strtoupper($fields[$i]['field']),'DATE') >= 1) {
                            echo '<A HREF="#" onClick="cal1.select(document.forms[0].to_'.$fields[$i]['field'].',\'anchor1\',\'ddMMyyyy\'); return false;" TITLE="Data" NAME="anchor1" ID="anchor1">'
                                .'<img src="calendar.gif" border=0></A>';
                        }
                }
                ?>
        </table>
        <input type = 'hidden' name = 'action' value = 'count'>
        <input type = 'submit' value = '<? echo $langfind ?>'>
        </form>
        <?
}


// change color of row every 3 rows
function row_color_change() {
        global $colorinterval, $color1, $color2;

        $colorinterval += 1;
        if ($colorinterval == 3) {
                $colorinterval = 0;
                return $color2;
        } else {
                return $color1;
        }
}

// save user selected criteria in search form
function save_search_list() {
        $program = $_SERVER['PHP_SELF'];
        unset ($_SESSION[$program]);
        foreach ($_GET as $index => $value) {
                if (substr($index,0,4) == 'from' || substr($index,0,2) == 'to') {
                        $_SESSION[$program][$index] = $value;
                }
        }
}

// return select statement based on user selected criteria in search form
function select_statement($table, $select=0) {
        $program = $_SERVER['PHP_SELF'];

        if ($select == 0) $sql = "SELECT count(*) FROM $table WHERE 1";
        else $sql = "SELECT * FROM $table WHERE 1";

        foreach ($_SESSION[$program] as $index => $value) {
                if (! empty($value)) {

                        if (substr_count(strtoupper($index),'DATE') >= 1)
                            $value = convert_user_to_mysql_date($value);

                        if (substr($index,0,4) == 'from') {
                                $field_name = substr($index, 5);
                                $sql .= " AND $field_name >= '$value'";
                        }
                        else { // it is a 'to' clause
                                $field_name = substr($index, 3);
                                $sql .= " AND $field_name <= '$value'";
                        }
                }
        }
        return $sql;
}

// return an URL text based on user selected criteria in search form
function retrieve_search_list() {

        $program = $_SERVER['PHP_SELF'];

        foreach ($_SESSION[$program] as $index => $value) {
                $text .= '&' . $index . '=' . $value;
        }

        return $text;
}

function charge_id_selection_box() {

        echo "\n<td><select name = 'ChargeID'>";
        $charge_list = get_charge_list();
        foreach ($charge_list as $charge_id) {
                echo "\n<option value='" . $charge_id['ChargeID']        . "'>" . $charge_id['ChargeType'] .
                " - " . $charge_id['ChargeID'] . " - " . $charge_id['ChargeDescription'];
        }
        echo "\n</select>";
}

function company_id_selection_box($blank=1) {

        echo "\n<td><select name='CompanyID'>";
        if ($blank == 1) echo "\n<option value=''>";
        $company_list = get_company_list();
        foreach ($company_list as $company) {
                echo "\n<option value='" . $company['CompanyID'] . "'>";
                echo $company['CompanyID'] . " - " . $company['CompanyName'];
        }
        echo "\n</select>";
}
?>
